package com.example.temp;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

import static com.google.code.kaptcha.Constants.KAPTCHA_SESSION_KEY;

/**
 * @author YAO
 * @create 2021-05-11 14:09
 */
@WebServlet(name = "RegisterServlet", value = "/registerServlet")
public class RegisterServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取session中的验证码
        String token = (String) req.getSession().getAttribute(KAPTCHA_SESSION_KEY);
        //删除session中的验证码
        req.getSession().removeAttribute(KAPTCHA_SESSION_KEY);

        String code = req.getParameter("code");
        if(token != null && token.equalsIgnoreCase(code)){
            // 获取用户名
            String username = req.getParameter("username");
            System.out.println("保存到数据库：" + username);
//        req.getRequestDispatcher("/ok.jsp").forward(req,resp);
            resp.sendRedirect(req.getContextPath() + "/ok.jsp");
        }else {
            System.out.println("请不要重复提交表单");
        }


    }
}
